﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;

namespace ChinaDynastyTours.Views
{
    public partial class HotelEvaluateForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtHotelEvaluateData;
        private string _TourgroupID { get; set; }

        public HotelEvaluateForm(string tourgroupId)
        {
            InitializeComponent();
            _TourgroupID = tourgroupId;
        }

        private void HotelEvaluateForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_TourgroupID))//如果没传进来团号
            {//初始化团下拉列表
                DataSet ds = TeamGroupCtrl.GetAllIDForTeamGroup();
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                        btnTeamGroupID.Properties.Items.Add(dr["TourGroupID"].ToString().Trim());
                }
            }
            else
            {
                btnTeamGroupID.Text = _TourgroupID;
                btnTeamGroupID.Enabled = false;
                hotelGridView.OptionsBehavior.Editable = false;
                hotelGridControl.DataSource = HotelEvaluateCtrl.GetHotelEvaluateData(_TourgroupID);
            }

        }

        private void btnTeamGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            _dtHotelEvaluateData = HotelEvaluateCtrl.GetHotelEvaluateData(btnTeamGroupID.Text);

            if (_dtHotelEvaluateData == null || _dtHotelEvaluateData.Rows.Count == 0)
            {
                string tourgroupName = TeamGroupCtrl.GetTeamGroupNameById(btnTeamGroupID.Text);

                //获取行程计划中旅行社、宾馆、目的地信息
                DataTable tmpDt = HotelEvaluateCtrl.GetHotelOrderForHotelEval(btnTeamGroupID.Text).Tables[0];

                foreach (DataRow dr in tmpDt.Rows)
                {
                    HotelEvaluateEntity entity = new HotelEvaluateEntity()
                    {
                        TourgroupID = btnTeamGroupID.Text,
                        TourgroupName = tourgroupName,
                        HotelID = int.Parse(dr["HotelID"].ToString()),
                        Hotel = BaseInfoCtrl.GetTeamProviderNameById(dr["HotelID"].ToString()),
                        CityName = dr["City"].ToString(),
                        Good = 0,
                        Middle = 0,
                        Common = 0,
                        Bad = 0,
                        Praise = 0,
                        Complain = 0,
                        Remark = "",
                    };
                    HotelEvaluateCtrl.CreateHotelEvaluateData(entity);
                }
                _dtHotelEvaluateData = HotelEvaluateCtrl.GetHotelEvaluateData(btnTeamGroupID.Text);
            }
            //加载数据到UI的gride
            hotelGridControl.DataSource = _dtHotelEvaluateData;
            _dtHotelEvaluateData.RowChanged += new DataRowChangeEventHandler(_dtHotelEvaluateData_RowChanged);
        }

        void _dtHotelEvaluateData_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {
                //string ProviderID = feedbackGridView.GetRowCellValue(feedbackGridView.FocusedRowHandle, "ProviderID").ToString();

                HotelEvaluateEntity entity = new HotelEvaluateEntity()
                {
                    EvaluateID = int.Parse(hotelGridView.GetRowCellValue(hotelGridView.FocusedRowHandle, "EvaluateID").ToString()),
                    TourgroupID = e.Row["TourgroupID"].ToString(),
                    TourgroupName = e.Row["TourgroupName"].ToString(),
                    CityName = e.Row["CityName"].ToString(),
                    Hotel = e.Row["Hotel"].ToString(),
                    HotelID = int.Parse(e.Row["HotelID"].ToString()),
                    Good = int.Parse(e.Row["Good"].ToString()),
                    Middle = int.Parse(e.Row["Middle"].ToString()),
                    Common = int.Parse(e.Row["Common"].ToString().ToLower()),
                    Bad = int.Parse(e.Row["Bad"].ToString()),
                    Praise = int.Parse(e.Row["Praise"].ToString()),
                    Complain = int.Parse(e.Row["Complain"].ToString()),
                    Remark = e.Row["Remark"].ToString()
                };
                HotelEvaluateCtrl.UpdateHotelEvaluateData(entity);
            }
        }
    }
}